import Vue from 'vue'
import store from '@/store'

Vue.prototype.$userInfo = () => {

	return store.state.userInfo;
}

Vue.prototype.$setUserInfo = (userInfo) => {

	store.state.userInfo = userInfo;

	uni.setStorageSync('userInfo', userInfo); //把state.userInfo存储起来

}

Vue.prototype.$isLogin = () => {

	return store.state.isLogin;
}

Vue.prototype.$setIsLogin = (loginStatus) => {

	store.state.isLogin = loginStatus;
	uni.setStorageSync('isLogin', loginStatus);
}

Vue.prototype.$needAuth = () => {

	return store.state.needAuth;
}

Vue.prototype.$setNeedAuth = (needAuth) => {

	store.state.needAuth = needAuth;

	uni.setStorageSync('needAuth', needAuth);
}


Vue.prototype.$longitude = () => {

	return store.state.longitude;
}

Vue.prototype.$setLongitude = (longitude) => {

	store.state.longitude = longitude;
}

Vue.prototype.$latitude = () => {

	return store.state.latitude;
}

Vue.prototype.$setLatitude = (latitude) => {

	store.state.latitude = latitude;
}


/**
 * 角色id
 */
Vue.prototype.$roleId = () => {

	return store.state.roleId;
}

Vue.prototype.$setRoleId = (roleId) => {

	store.state.roleId = roleId;

	Vue.prototype.$setTabBarList(roleId);

	uni.setStorageSync('roleId', roleId);
}



Vue.prototype.$login = () => {
	return new Promise((resolve, reject) => {
		// dispatch 用于调用 action中的方法
		Vue.prototype.$store.dispatch('login').then(res => {
			resolve(res);
		}).catch(err => {
			reject(err);
		})
	})
}

Vue.prototype.$auth = () => {
	return new Promise((resolve, reject) => {
		Vue.prototype.$store.dispatch('auth').then(res => {
			console.log("002")
			resolve(res);
		}).catch(err => {
			reject(err);
		})
	})
}


Vue.prototype.$accountLogin = (params) => {
	return new Promise((resolve, reject) => {

		// 登录成功
		Vue.prototype.$u.api.accountLogin(params).then(res => {
			uni.setStorageSync("token", res.data.token);

			if (res.code === 200) {
				Vue.prototype.$setIsLogin(true);
				Vue.prototype.$setNeedAuth(false);

			}
			// 返回值
			resolve(res);
		}).catch(error => {
			reject(error);
		})


	})
}


// 获取用户信息
Vue.prototype.$getUserInfo = () => {
	return new Promise((resolve) => {
		// 登录成功
		Vue.prototype.$u.api.getUserInfo().then(res => {
			if (res.code == 200) {
				Vue.prototype.$setUserInfo(res.data);
				Vue.prototype.$tmLogin();
				resolve(res.data);
			}
		});
	})
}

// 发送验证码
Vue.prototype.$sendMsg = (phone) => {
	return new Promise((resolve) => {
		// 登录成功
		Vue.prototype.$u.api.sendMsg({
			phone: phone
		}).then(res => {
			if (res.code == 200) {
				resolve(true);
			}
		});
	})
}

// wx支付
Vue.prototype.$wxPay = (param) => {
	return new Promise((resolve) => {
		let params = param;
		params.provider = 'wxpay';
		uni.requestPayment({
			provider: 'wxpay',
			orderInfo: '', // 订单数据
			timeStamp: param.timeStamp, // 时间戳从1970年1月1日至今的秒数，即当前的时间
			nonceStr: param.nonceStr, // 随机字符串，长度为32个字符以下
			package: param.package, // 统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=xx
			signType: param.signType, //签名算法，暂支持 MD5
			paySign: param.sign, // 签名
			success: function(res) {
				// 支付成功的回调中 创建绘本馆成功
				uni.showToast({
					title: '微信支付成功',
					icon: 'success',
					duration: 1500
				});
				resolve(res)
			},
			fail: function(err) {
				// 支付失败的回调中 用户未付款
				uni.showToast({
					title: '支付取消',
					duration: 1500,

				});
			}
		});
	})
}

// 获取openId
Vue.prototype.$getOpenId = () => {
	return new Promise((resolve) => {
		// 登录成功
		uni.login({
			provider: 'weixin',
			success(loginRes) {
				// 登录成功
				Vue.prototype.$u.api.getOpenId({
					code: loginRes.code
				}).then(res => {
					if (res.code == 200) {
						resolve(res.data)
					}
					// openId: resOpen.openId,
					// unionId:resOpen.unionId

				})
			}
		});
	})
}